﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HHCommon
{
    public class DirectoryFile
    {
        #region //目录管理 
        /// <summary>
        /// 创建指定目录
        /// </summary>
        /// <param name="filename"></param>
        public void Createdirctory(string Directoryname)
        {
            //string path = Server.MapPath("~/" + Directoryname);
            string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname;
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

        }
        /// <summary>
        /// 删除指定目录
        /// </summary>
        /// <param name="filename"></param>
        public void DeleteDirectiry(string Directoryname)
        {
            //string path = Server.MapPath("~/" + Directoryname);
            string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname;
            if (System.IO.Directory.Exists(path))
                System.IO.Directory.Delete(path);
        }
        /// <summary>
        /// 获取目录创建的时间
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string GetCreateDirectoryTime(string Directoryname)
        {
            //string path = Server.MapPath("~/" + Directoryname);
            string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname;
            string dt = string.Empty;
            if (System.IO.Directory.Exists(path))
            {
                dt = System.IO.Directory.GetCreationTime(path).ToString("yyyy-MM-dd HH:mm"); ;
            }
            return dt;
        }
        /// <summary>
        /// 移动目录
        /// </summary>
        /// <param name="directory1"></param>
        /// <param name="directory2"></param>
        public void DirectoryMove(string Directoryname1, string Directoryname2)
        {
            //string path1 = Server.MapPath("~/" + Directoryname1);
            //string path2 = Server.MapPath("~/" + Directoryname2);
            string path1 = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname1;
            string path2 = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname2;
            string dt = string.Empty;
            if (System.IO.Directory.Exists(path1) && System.IO.Directory.Exists(path2))
            {
                System.IO.Directory.Move(path1, path2);
            }
        }
        /// <summary>
        /// 获取指定目录中所有子目录的文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string[] GetDirectories(string Directoryname)
        {
            //string path = Server.MapPath("~/" + Directoryname);
            string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname;
            string[] Directorys = new string[2000];
            if (System.IO.Directory.Exists(path))
                Directorys = System.IO.Directory.GetDirectories(path);
            return Directorys;
        }
        #endregion

        #region //文件管理
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="Directoryname"></param>
        /// <param name="filename"></param>
        public void CreateFile(string Directoryname, string filename)
        {
            //string path = Server.MapPath("~/"+Directoryname+"/"+filename);
            string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname + "\\" + filename;
            if (!System.IO.File.Exists(path))
                System.IO.File.Create(path);
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="Directoryname"></param>
        /// <param name="filename"></param>
        public void DeleteFile(string Directoryname, string filename)
        {
            //string path = Server.MapPath("~/" + Directoryname + "/" + filename);
            string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname + "\\" + filename;
            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);
        }
        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="Directoryname1"></param>
        /// <param name="Directoryname2"></param>
        public void RemoveFile(string filename, string Directoryname1, string Directoryname2)
        {
            //string path1 = Server.MapPath("~/" + Directoryname1 + "/" + filename);
            //string path2 = Server.MapPath("~/" + Directoryname2);
            string path1 = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname1 + "\\" + filename;
            string path2 = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname2;
            if (System.IO.File.Exists(path1) && System.IO.Directory.Exists(path2))
                System.IO.File.Move(path1, path2);
        }
        /// <summary>
        /// 获取文件创建时间
        /// </summary>
        /// <param name="Directoryname"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public string GetCreateFileTime(string Directoryname, string filename)
        {
            //string path = Server.MapPath("~/" + Directoryname + "/" + filename);
            string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\" + Directoryname + "\\" + filename;
            string dt = null;
            if (System.IO.File.Exists(path))
                dt = System.IO.File.GetCreationTime(path).ToString("yyyy-MM-dd HH:mm");
            return dt;
        }
        #endregion
    }
}
